package com.note.service;

import com.note.mapper.UserMapper;
import com.note.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public void register(User user) {
        userMapper.insertUser(user);
    }

    @Override
    public String login(String loginname, String password, HttpSession session) {
        //查询用户
        User user = userMapper.findUserByLoginname(loginname);
        if (user != null){
            if (user.getPassword().equals(password)){
                session.setAttribute("user",user);
                return "101";  //登录成功
            }else {
                return "102";   //登录失败
            }
        }else {
            return "102";   //登录失败
        }
    }

    @Override
    public String findNameById(Integer id) {
        return userMapper.findNameById(id);
    }

    @Override
    public String findNameByName(String loginname) {
        //查询用户
        User user = userMapper.findUserByLoginname(loginname);
        if (user==null){
            return "001";  //用户名可用
        }else {
            return "000";   //用户名已存在
        }
    }

    @Override
    public User findUserById(Integer id) {
        return userMapper.findUserById(id);
    }

    @Override
    public String findAvatarById(Integer id) {
        return userMapper.findAvatarById(id);
    }

    @Override
    public void editpwd(User user) {
        userMapper.updatepwd(user);
    }

    @Override
    public User findByName(String loginname) {
        return userMapper.findUserByLoginname(loginname);
    }


}
